package l2ft.gameserver.model.entity.events.fightclubmanager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

import l2ft.gameserver.model.Player;
import l2ft.gameserver.model.entity.events.impl.AbstractFightClub;

public class FightClubLastStatsManager
{
	public static enum FightClubStatType
	{
		KILL_PLAYER("Kill Player"),
		KILL_PET("Kill Summon"),
		DIE("Die"),
		WON_EVENT("Winner"),
		CONQUARE_FLAG("Conquer Flag"),
		OPEN_CHEST("Open Chest"),
		CAPTURE_FLAG("Capture Flag"),
		AFK_MINUTES("Minutes spent AFK"),
		TIME_ON_EVENT("Minutes spent on event"),
		FIRST_PLACE("Gold Medal"),
		SECOND_PLACE("Silver Medal"),
		THIRD_PLACE("Bronze Medal");
		
		private final String _name;

		private FightClubStatType(String name)
		{
			_name = name;
		}
		
		public String getName()
		{
			return _name;
		}
	}
	
	
	private static Map<AbstractFightClub, FightClubLastStatsManager> _instances = new ConcurrentHashMap<>();
	
	private List<FightClubLastPlayerStats> _allStats = new Vector<>();
	//String - playerName
	private Map<String, List<FightClubLastPlayerStats>> _playerStats = new ConcurrentHashMap<>();
	
	
	public static void setNewStat(Player player, FightClubStatType type, int score, int badgesEarned, int realizationTime)
	{
		setNewStat(player, type.getName(), score, badgesEarned, realizationTime);
	}
	
	public static void setNewStat(Player player, String typeName, int score, int badgesEarned, int realizationTime)
	{
		if(!player.isInFightClub())
			return;
		
		FightClubLastPlayerStats stat = new FightClubLastPlayerStats(player, typeName, score, badgesEarned, realizationTime);
		
		FightClubLastStatsManager instance = getInstance(player.getFightClubEvent());
		//Adding to allStats and playerStats
		instance.getAllStats().add(stat);
		
		if(!instance.getAllPlayersStats().containsKey(player.getName()))
			instance.getAllPlayersStats().put(player.getName(), new ArrayList<FightClubLastPlayerStats>());
		
		List<FightClubLastPlayerStats> playerStats = instance.getAllPlayersStats().get(player.getName());
		playerStats.add(stat);
	}
	
	public List<FightClubLastPlayerStats> getAllStats()
	{
		return _allStats;
	}
	
	public Map<String, List<FightClubLastPlayerStats>> getAllPlayersStats()
	{
		return _playerStats;
	}
	
	public List<FightClubLastPlayerStats> getMyStats(Player player)
	{
		return _playerStats.get(player.getName());
	}
	
	public List<FightClubLastPlayerStats> getStatsByType(FightClubStatType type)
	{
		return getStatsByType(type.getName());
	}
	
	public List<FightClubLastPlayerStats> getStatsByType(String type)
	{
		List<FightClubLastPlayerStats> stats = new ArrayList<>();
		
		for(FightClubLastPlayerStats stat : _allStats)
			if(stat.getTypeName().equals(type))
				stats.add(stat);
		
		return stats;
	}
	
	/**
	 * @param achievements - 1, bestScors - 0
	 */
	public List<String> getAllTypes(boolean achievements)
	{
		List<String> allTypes = new ArrayList<>();
		for(FightClubLastPlayerStats stat : _allStats)
			if(!allTypes.contains(stat.getTypeName()))
				allTypes.add(stat.getTypeName());
		
		List<String> justNeeded = new ArrayList<>();
		for(String type : allTypes)
		{
			boolean isScore = false;
			for(FightClubStatType scoreTypes : FightClubStatType.values())
				if(scoreTypes.getName().equals(type))
					isScore = true;
			
			if(isScore && !achievements || !isScore && achievements)
			{
				justNeeded.add(type);
			}		
		}
		
		return justNeeded;
	}
	
	/**
	 * Setting position, best score and sorting all stats
	 */
	public void finishEvent()
	{
		SortRanking statsComparator = new SortRanking();
		Collections.sort(_allStats, statsComparator);
		
		List<String> allTypes = new ArrayList<>();
		
		for(FightClubLastPlayerStats stat : _allStats)
			if(!allTypes.contains(stat.getTypeName()))
				allTypes.add(stat.getTypeName());
		
		for(String type : allTypes)
		{
			int pos = 1;
			int bestScore = 0;
			for(FightClubLastPlayerStats stat : _allStats)
				if(stat.getTypeName().equals(type))
				{
					//Getting best score
					if(pos == 1)
					{
						bestScore = stat.getScore();
					}
					//Setting pos and bestScore
					stat.setPosition(pos);
					stat.setBestScore(bestScore);
					pos++;
				}
			
		}
	}
	
	private class SortRanking implements Comparator<FightClubLastPlayerStats> 
	{
		@Override
		public int compare(FightClubLastPlayerStats stat1, FightClubLastPlayerStats stat2)
		{
			return Integer.compare(stat2.getScore(), stat1.getScore());
		}
	}
	
	public static FightClubLastStatsManager getInstance(AbstractFightClub event)
	{
		if(event == null)
			return null;
		
		if(!_instances.containsKey(event))
			_instances.put(event, new FightClubLastStatsManager());
		
		return _instances.get(event);
	}
}
